
import { init, ZExcel as gol } from './init';
import { Inject } from './decorates/inject';
import { Factory } from './core/factory';
import { Render } from './core/render';
import { Config } from './core/config';
import { Draw } from './core/draw';
import { Events } from './core/events';
import { Select } from './core/select';
import './styles/index.less';
import "reflect-metadata";
import {Subject, Observable, from} from 'rxjs';
import { mergeMap } from 'rxjs/operators';
import { TextEditor } from './editors';
import { EditorManage } from './core/editor-manage';
import { Loggin } from './core/logging';
import { Scroll } from './core/scroll';
import { state } from './decorates/state';
init();
@Inject()
export class ZExcel{
    constructor(
        protected rd: Render, 
        protected conf: Config, 
        protected draw: Draw, 
        protected  ev: Events, 
        protected select: Select, 
        public editorManager: EditorManage,
        private loggin: Loggin,
        private render: Render,
        private scroll: Scroll
        ) {
        this.test();
    }
    protected init(container: HTMLElement, conf: {[key: string]: any}) {
        this.conf.setConfig(conf);
        this.draw.setContainer(container);
        this.editorManager.loadEditor(<any>TextEditor);
        this.rd.render();
    }
    private test() {
        let obs1 = new Subject().pipe(() => {
            return from('dd');
        });
        
        obs1.subscribe(r => {
            console.log(r);
        });
    }
    @state()
    public merge(start?: {row: number, col: number}, end?: {row: number, col: number}) {   
        if(start == undefined || end == undefined) {
            if(this.select.selectRanges[0] == undefined) {
                this.loggin.warn('没有选中');
                return false;
            }
            let range = this.select.selectRanges[0];
            start = {
                row: Math.min(range.start.row, range.end.row),
                col: Math.min(range.start.col, range.end.col)
            }
            end = {
                row: Math.max(range.start.row, range.end.row),
                col: Math.max(range.start.col, range.end.col)
            }
        }
        if(start.row == end.row && start.col == end.col) return false;
        let merge = {
            col: start.col,
            row: start.row,
            rowspan: end.row - start.row + 1,
            colspan: end.col - start.col + 1
        }
        this.conf.traverseCells(start, end, (cell) => {
            cell.merge = {...merge};
            return cell;
        });
        this.render.render(start, end)
    }
    public splite(start?: {row: number, col: number}, end?: {row: number, col: number}) {
        if(start == undefined || end == undefined) {
            if(this.select.selectRanges[0] == undefined) {
                this.loggin.warn('没有选中');
                return false;
            }
            let range = this.select.selectRanges[0];
            start = {
                row: Math.min(range.start.row, range.end.row),
                col: Math.min(range.start.col, range.end.col)
            }
            end = {
                row: Math.max(range.start.row, range.end.row),
                col: Math.max(range.start.col, range.end.col)
            }
        }
        if(start.row == end.row && start.col == end.col) return false;
        this.conf.traverseCells(start, end, (cell) => {
            cell.merge = {
                row: cell.row,
                col: cell.col,
                rowspan: 1,
                colspan: 1
            };
            return cell;
        });
        this.render.render(start, end);
    }
}
gol.create = function(container: HTMLElement, conf: any) {
    let instance = Factory(ZExcel, container, conf);
    instance.init(container, conf);
    return instance;
}
interface ICellPosition {
    row: number,
    col: number
}

